package com.ruoyi.media.publishModelConfig.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.publishModelConfig.domain.bo.MediaPublishModelConfigBo;
import com.ruoyi.media.publishModelConfig.domain.vo.MediaPublishModelConfigVo;
import com.ruoyi.media.publishModelConfig.domain.MediaPublishModelConfig;
import com.ruoyi.media.publishModelConfig.mapper.MediaPublishModelConfigMapper;
import com.ruoyi.media.publishModelConfig.service.IMediaPublishModelConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 发布模板配置Service业务层处理
 *
 * @author dqw
 * @date 2023-03-27
 */
@RequiredArgsConstructor
@Service
public class MediaPublishModelConfigServiceImpl extends ServiceImpl<MediaPublishModelConfigMapper,MediaPublishModelConfig> implements IMediaPublishModelConfigService {

    private final MediaPublishModelConfigMapper baseMapper;

    /**
     * 查询发布模板配置
     */
    @Override
    public MediaPublishModelConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询发布模板配置列表
     */
    @Override
    public TableDataInfo<MediaPublishModelConfigVo> queryPageList(MediaPublishModelConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MediaPublishModelConfig> lqw = buildQueryWrapper(bo);
        Page<MediaPublishModelConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询发布模板配置列表
     */
    @Override
    public List<MediaPublishModelConfigVo> queryList(MediaPublishModelConfigBo bo) {
        LambdaQueryWrapper<MediaPublishModelConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaPublishModelConfig> buildQueryWrapper(MediaPublishModelConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaPublishModelConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaPublishModelConfig::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaPublishModelConfig::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getFilePath()), MediaPublishModelConfig::getFilePath, bo.getFilePath());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatform()), MediaPublishModelConfig::getPlatform, bo.getPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), MediaPublishModelConfig::getAccountId, bo.getAccountId());
        return lqw;
    }

    /**
     * 新增发布模板配置
     */
    @Override
    public Boolean insertByBo(MediaPublishModelConfigBo bo) {
        MediaPublishModelConfig add = BeanUtil.toBean(bo, MediaPublishModelConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改发布模板配置
     */
    @Override
    public Boolean updateByBo(MediaPublishModelConfigBo bo) {
        MediaPublishModelConfig update = BeanUtil.toBean(bo, MediaPublishModelConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaPublishModelConfig entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除发布模板配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
